package jiqiao;

import java.util.Arrays;

/**
 * @author understand
 * @description:
 * @create 2025/5/18 15:42
 */
public class NextPermutation {
    public static void main(String[] args) {
        int[] nums = {1,2};
        nextPermutation(nums);
        System.out.println(Arrays.toString(nums));
    }
    public static void nextPermutation(int[] nums) {
        int n = nums.length-1,flag = -1;
        for (int i = n; i >0; i--) {
            //找到第一个小于后面一个的数
            if (nums[i]>nums[i-1]) {
                flag = i - 1;
                break;
            }
        }
        if (flag == -1) {
            Arrays.sort(nums);
        }else {
            for (int i = n; i >flag; i--) {
                if (nums[i]>nums[flag]) {
                    int temp = nums[i];
                    nums[i] = nums[flag];
                    nums[flag] = temp;
                    break;
                }
            }
            Arrays.sort(nums,flag+1,n+1);
        }
    }
}
